package com.plugin.manage.interceptor;

import com.plugin.core.bean.LogAop;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;



@Component
@Slf4j
public class AuthSecuriteInterceptor implements HandlerInterceptor {



    ThreadLocal<Long> timeStore=new ThreadLocal<>();

    @LogAop(remark = "登录校验拦截器")
    public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object object) throws Exception {
        timeStore.set(System.currentTimeMillis());
        // 如果不是映射到方法直接通过
        if(!(object instanceof HandlerMethod)){
            return true;
        }
        HandlerMethod handlerMethod=(HandlerMethod)object;
        // 判断如果请求的类是swagger的控制器，直接通行。
        if(handlerMethod.getBean().getClass().getName().equals("springfox.documentation.swagger.web.ApiResourceController")){
            return true;
        }
        log.info("离开token校验拦截器......");

        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) {


    }

    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        long duration = System.currentTimeMillis() - timeStore.get();
        log.info("请求处理耗时:........... {}ms", duration);
        HandlerInterceptor.super.afterCompletion(httpServletRequest, httpServletResponse, o, e);
    }


}
